package com.course.servlet;

import com.course.dao.GuanliyuanCourseDAO;
import com.course.dao.TeacherDAO;
import com.course.entity.Course;
import com.course.entity.Teacher;
import com.course.service.CourseService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/guanliyuan_publish_course")
public class GuanliyuanPublishCourseServlet extends HttpServlet {
    private CourseService courseService;
    private TeacherDAO teacherDAO;

    @Override
    public void init() throws ServletException {
        super.init();
        this.courseService = new CourseService(new GuanliyuanCourseDAO());
        this.teacherDAO = new TeacherDAO();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String courseName = request.getParameter("courseName");
        String courseDescription = request.getParameter("courseDescription");
        String teacherName = request.getParameter("teacherName");
        String maxCapacityStr = request.getParameter("maxCapacity");

        try {
            Teacher teacher = teacherDAO.findByName(teacherName);

            if (teacher == null) {
                request.getSession().setAttribute("errorMessage", "找不到该老师，请确认老师姓名是否正确。");
                response.sendRedirect(request.getContextPath() + "/guanliyuan_publish_course.jsp");
                return;
            }

            int teacherId = teacher.getId();
            int maxCapacity = Integer.parseInt(maxCapacityStr);

            Course newCourse = new Course();
            newCourse.setCourseName(courseName);
            newCourse.setCourseDescription(courseDescription);
            newCourse.setTeacherId(teacherId);
            newCourse.setMaxCapacity(maxCapacity);

            boolean success = courseService.addCourse(newCourse);

            if (success) {
                request.getSession().setAttribute("successMessage", "课程发布成功！");
            } else {
                request.getSession().setAttribute("errorMessage", "课程发布失败，请检查输入或联系管理员。");
            }
        } catch (NumberFormatException e) {
            request.getSession().setAttribute("errorMessage", "输入格式错误，请检查最大容量。");
        } catch (Exception e) {
            request.getSession().setAttribute("errorMessage", "发布课程时发生错误：" + e.getMessage());
            e.printStackTrace();
        }

        response.sendRedirect(request.getContextPath() + "/guanliyuan_publish_course.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Clear any previous messages from the session when the page is loaded via GET
        request.getSession().removeAttribute("successMessage");
        request.getSession().removeAttribute("errorMessage");

        request.getRequestDispatcher("/guanliyuan_publish_course.jsp").forward(request, response);
    }
} 